﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using nvn.Library.Patterns.MVP;
using System.Data;
using System.Configuration;

namespace Main.Model.DATA
{
    public class DBAdmin
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }


        /// <summary>
        /// Inserts a row in the ad_Admin table. Returns rows affected count.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        /// <param name="username"> username </param>
        /// <param name="password"> password </param>
        /// <returns>int</returns>
        public static int Create(
            Guid idGuid,
            string username,
            string password)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ad_Admin_Insert", 3);
            sph.DefineSqlParameter("@IdGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, idGuid);
            sph.DefineSqlParameter("@username", SqlDbType.VarChar, 50, ParameterDirection.Input, username);
            sph.DefineSqlParameter("@password", SqlDbType.VarChar, 50, ParameterDirection.Input, password);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the ad_Admin table. Returns true if row updated.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        /// <param name="username"> username </param>
        /// <param name="password"> password </param>
        /// <returns>bool</returns>
        public static bool Update(
            Guid idGuid,
            string username,
            string password)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ad_Admin_Update", 3);
            sph.DefineSqlParameter("@IdGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, idGuid);
            sph.DefineSqlParameter("@username", SqlDbType.VarChar, 50, ParameterDirection.Input, username);
            sph.DefineSqlParameter("@password", SqlDbType.VarChar, 50, ParameterDirection.Input, password);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the ad_Admin table. Returns true if row deleted.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            Guid idGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ad_Admin_Delete", 1);
            sph.DefineSqlParameter("@IdGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, idGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the ad_Admin table.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        public static IDataReader GetOne(
            Guid idGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ad_Admin_SelectOne", 1);
            sph.DefineSqlParameter("@IdGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, idGuid);
            return sph.ExecuteReader();

        }
        public static int GetLogin(string username, string pass)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ad_Admin_Login", 2);
            sph.DefineSqlParameter("@username", SqlDbType.VarChar, 50, ParameterDirection.Input, username);
            sph.DefineSqlParameter("@password", SqlDbType.VarChar, 50, ParameterDirection.Input, pass);
            return Convert.ToInt32(sph.ExecuteScalar());
        }
        /// <summary>
        /// Gets a count of rows in the ad_Admin table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "ad_Admin_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the ad_Admin table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "ad_Admin_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the ad_Admin table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount();

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ad_Admin_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }

    }

}


